package br.com.emmis.tercons;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.analytics.tracking.android.EasyTracker;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link MyIntentionFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link MyIntentionFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class MyIntentionFragment extends Fragment {

    private OnFragmentInteractionListener mListener;
    private EditText mEdIntentionName;
    private EditText mEdIntentionCity;
    private EditText mEdIntentionText;

    private EasyTracker mEasyTracker;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyIntentionFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MyIntentionFragment newInstance() {
        MyIntentionFragment fragment = new MyIntentionFragment();
        return fragment;
    }
    public MyIntentionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEasyTracker = EasyTracker.getInstance(getActivity());
    }

    @Override
    public void onStop() {
        super.onStop();
        mEasyTracker.activityStop(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();
        mEasyTracker.activityStart(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_intention, container, false);

        mEdIntentionName = (EditText) view.findViewById(R.id.etIntentionName);
        mEdIntentionCity = (EditText) view.findViewById(R.id.etIntentionCity);
        mEdIntentionText = (EditText) view.findViewById(R.id.etIntentionText);

        ((Button) view.findViewById(R.id.btnSendIntention)).setOnClickListener(new
                                                                                       View.OnClickListener() {
                                                                                           @Override
                                                                                           public void onClick(View view) {
	if(mListener==null) {
		return;
	}	
mListener.onIntentionSent(mEdIntentionName.getText().toString(), mEdIntentionCity.getText().toString(), mEdIntentionText.getText().toString());
                                                                                           }
                                                                                       });
        return view;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onIntentionSent(String name, String city, String prece);
    }

}
